package parseAndHandleTool;

import com.oocl.crm_system.Customer;
import com.oocl.crm_system.DoublyList;
import com.oocl.crm_system.Node;

public class ParseCmdForSort extends ParseCmd{
	/**
	 * 
	 * @param list
	 * @param inputByConsole
	 * @return void
	 * @Description show user all customer's message which is sorted
	 */
	public void parseCmdForSort(DoublyList<Customer> list, String inputByConsole){
		String input = inputByConsole;
		String[] buf = input.split(" ");
		String byNameOrId = buf[1];
		
		Customer[]  customers = new Customer[list.nodeNum];
		
		for(int i = 0; i < list.nodeNum; i++){
			Node<Customer> currentNode = list.findNodeByIndex(i);
			customers[i] = currentNode.value;
		}
		
		if(byNameOrId.equals("name")){
			sortNameTool(customers);
		}else{
			sortIdTool(customers);
		}
	}
	
	/**
	 * 
	 * @param a
	 * @return void
	 * @Description called by parseCmdForSort
	 */
	private void sortIdTool(Customer[] a){
		for(int i = 0; i< a.length - 1; i++){
			for(int j = 0; j < a.length - 1 - i; j++){
				if(a[j].getId() > a[j+1].getId()){
					Customer tmp = a[j+1];
					a[j+1] = a[j];
					a[j] = tmp;
				}
			}
		}
		for(int k = 0; k < a.length; k++){
			System.out.println(a[k].getId() + " " + a[k].getName() + " " + a[k].getSex() + " " + 
					a[k].getBirthday() + " " + a[k].getAddress() + " " + a[k].getCall());
		}
	}
	
	/**
	 * 
	 * @param a
	 * @return void
	 * @Description called by parseCmdForSort
	 */
	private void sortNameTool(Customer[] a){
		System.out.println("sortNameTool----------------test");
		for(int i = 0; i< a.length - 1; i++){
			for(int j = 0; j < a.length - 2 - i; j++){
				if(a[j].getName().compareTo(a[j+1].getName()) > 0){
					Customer tmp = a[j+1];
					a[j+1] = a[j];
					a[j] = tmp;
				}
			}
		}
		for(int k = 0; k < a.length; k++){
			System.out.println(a[k].getId() + " " + a[k].getName() + " " + a[k].getSex() + " " + 
					a[k].getBirthday() + " " + a[k].getAddress() + " " + a[k].getCall());
		}
	}
}
